package com.yilijk.base.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yilijk.base.R;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.GlideUtil;
import com.yilijk.base.utils.ScreenUtils;


public class LoadingDialog extends Dialog {

    private int loadingWidth = 0;

    private int loadingHPadding, loadingWPadding = 0;

    private LinearLayout linearLayout;

    private TextView msgTextView;

    private Context context;

    public LoadingDialog(Context context) {
        super(context);
        this.context = context;
        getWindow().setDimAmount(0f);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        loadingWidth = ScreenUtils.dip2px(context, 60);
        loadingHPadding = ScreenUtils.dip2px(context, 15);
        loadingWPadding = ScreenUtils.dip2px(context, 36);
        setContentView(initView());
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private View initView() {

        linearLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.dialog_loading, null, false);
        msgTextView = linearLayout.findViewById(R.id.msg_tv);

//        LinearLayout.LayoutParams proll = (LinearLayout.LayoutParams) loadingView.getLayoutParams();
//        proll.width = 850;
//        proll.height = 850;
//        proll.width = (int) (getWindow().getWindowManager().getDefaultDisplay().getWidth() * 0.7);
//        proll.height = (int) (getWindow().getWindowManager().getDefaultDisplay().getWidth() * 0.7);
//        loadingView.setLayoutParams(proll);

//        LinearLayout.LayoutParams msgTvParams = (LinearLayout.LayoutParams) msgTextView.getLayoutParams();
//        msgTvParams.topMargin = -(int) (getWindow().getWindowManager().getDefaultDisplay().getWidth() * 0.27);
//        msgTextView.setLayoutParams(msgTvParams);
        setCanceledOnTouchOutside(false);
//        AnimationDrawable drawable = new AnimationDrawable();
//        for (int i = 0; i < 1; i++) {
//            Bitmap bitmap = BitmapFactory.decodeFile("android.resource://com.yljk.patient/drawable/loading_000"+String.format("%02d",i));
//            Drawable  x  = new BitmapDrawable(bitmap);
//            drawable.addFrame(x, 50);
//
//        }
//        drawable.addFrame(x1, 300);

        //设置手否重复播放，false为重复
//        drawable.setOneShot(false);
//        loadingView.setImageDrawable(drawable);
        return linearLayout;
    }


    @Override
    public void show() {
        try {
            if (this != null) {
                show("加载中...");
//                msgTextView.setVisibility(View.GONE);
//                msgTextView.setText("");
//                int padding = ScreenUtils.dip2px(context,10);
//                if (linearLayout.getPaddingTop() != padding) {
//                    ViewGroup.LayoutParams proll = linearLayout.getLayoutParams();
//                    proll.width = loadingWidth;
//                    proll.height = loadingWidth;
//                    linearLayout.setLayoutParams(proll);
//                    linearLayout.setPadding(padding, padding, padding, padding);
//                }
//                super.show();
            }
        } catch (Throwable e) {

        }

    }

    public void show(String msg) {
        try {
            if (this != null) {
//                if (TextUtils.isEmpty(msg)){
//                    show();
//                    return;
//                }
                msgTextView.setVisibility(View.VISIBLE);
                msgTextView.setText(msg);
                if (linearLayout.getPaddingTop() != loadingHPadding) {
                    ViewGroup.LayoutParams proll = linearLayout.getLayoutParams();
                    proll.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    proll.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    linearLayout.setLayoutParams(proll);
                    linearLayout.setPadding(loadingWPadding, loadingHPadding, loadingWPadding, loadingHPadding);
                }
                super.show();
            }
        } catch (Throwable e) {

        }

    }

    @Override
    public void dismiss() {
        super.dismiss();

    }
}

